package com.yonyou.pmclouds.print.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.iuap.print.client.utils.JsonUtil;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.StorageFactory;
import com.yonyou.pmclouds.basecom.view.DisableWrap;
import com.yonyou.pmclouds.businessobject.util.BusinessObjectConsts;
import com.yonyou.pmclouds.businessobject.util.PrintPreviewObj2Html;
import com.yonyou.pmclouds.visitcard.entity.VisitCardVO;
import com.yonyou.pmclouds.visitcard.rmiitf.VisitCardQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/rest/print/businessData")
public class BusinessDataMobileController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private VisitCardQuery visitCardQuery;

    @Autowired
    private StorageFactory storageFactory;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    // 体验馆特殊情况设置相片的高度
    @Value("${print.visitcard.height}")
    private String height;
    /**
     * 参观卡业务数据接口
     */
    @RequestMapping("/visitCard")
    @DisableWrap
    public Map<String, Object> visitCardBusinessData(HttpServletRequest request) throws BusinessException {
        Map<String, Object> result = new HashMap<>();
        String id = (String) JsonUtil.jsonToMap(request.getParameter("params")).get("id");
        VisitCardVO visitCardVO = visitCardQuery.queryById(id);
        visitCardVO.setStrVisitDate(sdf.format(visitCardVO.getVisitDate()));
        PrintPreviewObj2Html previewObj2Html = new PrintPreviewObj2Html(PrintPreviewObj2Html.BASE_PREFIX,
                PrintPreviewObj2Html.IMG, visitCardVO.getPictures().length);
        String picHtml = previewObj2Html.urls2HtmlForVisitCard(storageFactory.getStorageUtil().signUrlForMultiPictures(visitCardVO.getPictures(), null, request), height);
        visitCardVO.setPictureAddress(picHtml);
        result.put(BusinessObjectConsts.PM_VISITCARD, new VisitCardVO[]{visitCardVO});
        return result;
    }
}
